<template>
	<view>
		<view class="header flex">
			<image :src="imgurl+'search.png'"></image>
			<input class="inputcss" :class="[showcel==false?'':'otherinput']" placeholder="输入关键词搜索题库" placeholder-class="placeholder"
			 @focus="getFocus" confirm-type="search" @confirm="getTitle" v-model="searchtitle" />
			<view class="cancel" @tap="Cancel" v-if="showcel">取消</view>
		</view>
		<view class="swiper-tab flex">
			<view class="swiper-tab-list" :class="[currentTab==0 ? 'on' : '']" :data-current="0" @tap="swichNav">我收藏的</view>
			<view class="swiper-tab-list" :class="[currentTab==1 ? 'on' : '']" :data-current="1" @tap="swichNav">我创建的</view>
		</view>
		<swiper :current="currentTab" :style="{height:aheight+'px'}">
			<!-- 我收藏的 -->
			<swiper-item>
				<scroll-view scroll-y="true" :style="{height:aheight+'px'}">
					<view class="main-item" v-for="(item,index) in list" :key="index" @tap="chooseDetail(item.wenid)">
						<view v-if="(item.tishu && item.tishu>0) || (item.zongshu && item.zongshu > 0)">
							<view class="item-top flex">
								<view class="top-left flex">
									<text>{{item.namewenid}}</text>
									<span>共{{item.zongshu}}题</span>
								</view>
							</view>
							<view class="main-show flex">
								<view class="show-left">
									<text>答卷人数:</text>
									<span>{{item.darenshu}}</span>
								</view>
								<view class="show-right">
									<text>有效答卷数:</text>
									<span>{{item.youxiaojuan}}</span>
								</view>
							</view>
							<view class="item-content">{{item.text}}</view>
							<view class="item-bottom flex">
								<view class="bottom-left">
									<text>收藏时间:</text>
									<span>{{item.startdate}}</span>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
			<!-- 我创建的 -->
			<swiper-item>
				<scroll-view scroll-y="true" :style="{height:aheight+'px'}">
					<view class="main-item" v-for="(item,index) in createlist" :key="index" @tap="chooseDetail(item.wenid)">
						<view>
							<view class="item-top flex">
								<view class="top-left flex">
									<text>{{item.namewenid}}</text>
									<span>共{{item.zongshu}}题</span>
								</view>
								<view class="top-right flex">
									<text :class="[item.fa==1?'': 'showstate']"></text>
									<span>{{item.fa==1?'未发布':'已发布'}}</span>
								</view>
							</view>
							<view class="main-show flex">
								<view class="show-left">
									<text>答卷人数:</text>
									<span>{{item.darenshu}}</span>
								</view>
								<view class="show-right">
									<text>有效答卷数:</text>
									<span>{{item.youxiaojuan}}</span>
								</view>
							</view>
							<view class="item-content">{{item.text}}</view>
							<view class="item-bottom flex">
								<view class="bottom-left">
									<text>创建时间:</text>
									<span>{{item.date==null?'':item.date}}</span>
								</view>
								<view class="bottom-left">
									<text>{{item.state==2?'结束时间':'修改时间'}}:</text>
									<span>{{item.state=='2'?item.update:item.update}}</span>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import api from "@/common/api.js";
	import {
		baseURL
	} from "@/common/http.js";
	import app from "../../../App.vue"
	export default {
		data() {
			return {
				imgurl: '',
				showcel: false,
				currentTab: 0,
				page: 1,
				aheight: "",
				ids: '',
				list: [],
				createlist: [],
				searchtitle: ''
			};
		},
		methods: {
			//搜索(问卷名字)
			getTitle() {
				let that = this
				if (this.currentTab == 1) {
					api.quesList({
						api: 'wenpeg',
						shu: '',
						order: 'id desc',
						table: 3,
						del: 1,
						namewenid: this.searchtitle,
						name: uni.getStorageSync('message')[0].id,
						page: this.page
					}).then(res => {
						if (res.code == 200) {
							if (res.date.length < 15) {
								that.moreTitle = ""
							}
							that.createlist = res.date
						}
						let arr = this.createlist
						let newArr = arr.map((item, index) => {
							item.showIcon = false
							return item
						})
						this.createlist = newArr
					}).catch(err => {
						console.log(err)
					})
				} else if (this.currentTab == 0) {
					api.look({
						api: 'wensou',
						namewenid: this.searchtitle,
						nameid: uni.getStorageSync('message')[0].id
					}).then(res => {
						if (res.code == 200) {
							this.list = res.date
						}
					}).catch(err => {
						console.log(err)
					})
				}
			},
			getFocus() {
				this.showcel = true
			},
			Cancel() {
				this.showcel = false
			},
			//点击tab切换
			swichNav(e) {
				var that = this;
				if (this.currentTab === e.target.dataset.current) {
					return false
				} else {
					this.currentTab = e.target.dataset.current
				}
				console.log(this.currentTab, 'TAB')
			},
			chooseDetail(id) {
				uni.navigateTo({
					url: "../chooseDetail/chooseDetail?wenid=" + id + '&ids=' + this.ids
				})
			}
		},
		onShareAppMessage(options) {
			let shareObj = {
				success: (res) => {
					if (res.errMsg == 'shareAppMessage:ok') {
						uni.showToast({
							title: '分享成功',
							icon: 'success',
							duration: 1500
						})
					}
				},
				fail: () => {
					if (res.errMsg == 'shareAppMessage:fail cancel') {
						uni.showToast({
							title: '取消分享',
							icon: 'none',
							duration: 2000
						})
					} else if (res.errMsg == 'shareAppMessage:fail') {
						// 转发失败，其中 detail message 为详细失败信息
					}
				},
				complete: () => {
					// 转发结束之后的回调（转发成不成功都会执行）
				}
			}
		},
		onLoad(options) {
			this.ids = options.id
			this.imgurl = app.globalData.imgurls
		},
		onShow() {
			let that = this
			uni.getSystemInfo({
				success(res) {
					that.aheight = res.windowHeight * 0.85
				}
			})
			api.quesList({
				api: 'wenpeg',
				shu: '',
				order: 'id desc',
				table: 3,
				del: 1,
				name: uni.getStorageSync('message')[0].id,
				page: this.page
			}).then(res => {
				if (res.code == 200) {
					if (res.date.length < 15) {
						that.moreTitle = ""
					}
					that.createlist = res.date
				}
				let arr = this.createlist
				let newArr = arr.map((item, index) => {
					item.showIcon = false
					return item
				})
				this.createlist = newArr
			}).catch(err => {
				console.log(err)
			})
			//我创建的
			api.look({
				api: 'wensou',
				nameid: uni.getStorageSync('message')[0].id
			}).then(res => {
				if (res.code == 200) {
					this.list = res.date
				}
			}).catch(err => {
				console.log(err)
			})
		},
	}
</script>

<style lang="scss">
	page {
		background: #F0F0F0;
		font-weight: 500rpx;
		font-family: PingFang SC;
	}

	.placeholder {
		color: #999;
	}

	.header {
		justify-content: center;
		padding: 20rpx 0;
		background: #fff;
		position: relative;

		image {
			width: 50rpx;
			height: 50rpx;
			position: absolute;
			left: 5%;
			top: 30rpx;
		}

		.inputcss {
			background: F0F0F0;
			width: 690rpx;
			height: 60rpx;
			line-height: 60rpx;
			border-radius: 30rpx;
			color: #333;
			padding-left: 7%;
			box-sizing: border-box;
			background: #F0F0F0;
		}

		.otherinput {
			width: 615rpx;
		}

		.cancel {
			color: #666;
			font-size: 32rpx;
			margin-left: 10rpx;
		}
	}

	.swiper-tab {
		background: #fff;
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		justify-content: space-between;

		.swiper-tab-list {
			width: 50%;
			color: #333;
			font-size: 32rpx;
			text-align: center;
		}
	}

	.swiper-tab .on {
		color: #1F9FFB;
		position: relative
	}

	.on:after {
		content: "";
		display: block;
		height: 8rpx;
		width: 155rpx;
		background: #1F9FFB;
		position: absolute;
		bottom: 0;
		left: 30%;
		border-radius: 16rpx
	}

	.main-item {
		width: 690rpx;
		margin: 30rpx auto;
		border-radius: 5rpx;
		background: #fff;
		padding: 15rpx 20rpx;
		box-sizing: border-box;

		.item-top {
			justify-content: space-between;

			.top-left {
				text {
					font-size: 36rpx;
					color: #333;
					margin-right: 5rpx;
				}

				span {
					font-size: 24rpx;
					color: #666;
				}
			}

			.top-right {
				text {
					width: 15rpx;
					height: 15rpx;
					display: inline-block;
					background: #999;
					border-radius: 50%;
					margin-right: 5rpx;
				}

				span {
					font-size: 20rpx;
					color: #999;
				}
			}
		}

	}

	.item-name {
		height: 50rpx;
		line-height: 40rpx;
		font-size: 30rpx;
	}

	.main-show {
		font-size: 30rpx;
		margin: 10rpx 0;

		.show-left {
			margin-right: 30rpx;

			text {
				color: #333;
				margin-right: 5rpx;
			}

			span {
				color: #209FFC;
			}
		}

		.show-right {
			text {
				color: #333;
				margin-right: 5rpx;
			}

			span {
				color: #209FFC;
			}
		}
	}

	.item-content {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
		overflow: hidden;
		color: #999;
		font-size: 24rpx;
		line-height: 30rpx;
	}

	.item-bottom {
		font-size: 20rpx;
		line-height: 30rpx;
		color: #666;
		margin: 8rpx 0;

		.bottom-left {
			margin-right: 20rpx;
		}
	}
</style>
